<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Goods;
use app\yunda\model\Goods_type;
use think\Db;

class Shop extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $data = Db::view('yd_score_goods',true)
             ->view('yd_goods_type',['good_type_name'],'yd_goods_type.id=yd_score_goods.good_type','left')
            ->where($map)
            ->order($order)
            ->paginate()->each(function($item){
//                $arr=['否','是'];
//                $item['is_recommend']=$arr[$item['is_recommend']];
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_score_goods')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['good_name','商品名称'],
                ['good_type_name','商品分类'],
                ['good_img','商品图片','picture'],
                ['need_score','兑换积分'],
                ['describe','商品描述'],
                ['stock','库存'],
                ['sort','排序'],
                ['status','是否显示','switch'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['good_type_name' => 'yd_goods_type'])
            ->setSearch(['id' => 'ID', 'good_name' => '商品名称'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Goods');
            if($result!==true) $this->error($result);
            // 第三部 存储
            Goods::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','good_name','商品名称','必须'],
                ['select','good_type','商品分类','必须',Goods_type::get_list_goodType('admin')],
                ['image','good_img','商品图片','必须'],
                ['number','need_score','兑换积分','必须'],
                ['textarea','describe','商品描述'],
                ['number','stock','库存','必须'],
                ['radio','status','是否推荐','',['否','是'],1],
                ['number','sort','排序','','',0],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Goods');
            if($result!==true) $this->error($result);
            Goods::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = Goods::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
                ['text','good_name','商品名称','必须'],
                ['select','good_type','商品分类','必须',Goods_type::get_list_goodType('admin')],
                ['image','good_img','商品图片','必须'],
                ['number','need_score','兑换积分','必须'],
                ['textarea','describe','商品描述'],
                ['number','stock','库存','必须'],
                ['radio','status','是否显示','',['否','是']],
                ['number','sort','排序',''],
            ])
            ->setFormData($data)
            ->fetch();
    }
}